package com.gexin.model.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.gexin.common.utils.SQLUtils;
import com.gexin.model.system.base.BaseAdminRes;
import com.jfinal.plugin.activerecord.Page;
/**
 * 业务名称:  t_admin_res(权限资源表) 
 */
@SuppressWarnings("serial")
public class AdminRes extends BaseAdminRes<AdminRes> {
	public static final AdminRes dao = new AdminRes();


	public List<String> getUrls() {
		String sql = "select url from t_admin_res where url is not null and pid !=0";
		List<String>  list =new ArrayList<String>();
		List<AdminRes> reslist =  dao.find(sql);
		for(AdminRes res : reslist){
			list.add(res.getUrl());
		}
		return list;
	}
	
	/**
	 * 获取所有顶级的节点
	 * @return
	 */
	public  List<AdminRes> getRootRes(){
		return dao.find("select * from t_admin_res where pid  is null order by sort");
	}
	
	
	/**
	 * 获取所有顶级的节点分页
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	public Page<AdminRes> paginate(int pageNumber, int pageSize) {
		SQLUtils sql  = new SQLUtils(" from t_admin_res where pid  is null ");
		sql.append(" order by sort ");
		return paginate(pageNumber, pageSize, " select *  ", sql.toString());
	}
	
	/**
	 * 获取管理员的权限 全部的权限组
	 * @return
	 */
	public Map<AdminRes,List<AdminRes>> adminRole(){
		Map<AdminRes,List<AdminRes>> map =new LinkedHashMap<AdminRes, List<AdminRes>>();
		List<AdminRes> list = dao.find("select * from t_admin_res where pid is null ORDER BY sort  ");
		for(AdminRes res :   list ){
			List<AdminRes> reslist =  dao.find("select * from t_admin_res where pid=? and type=1 ORDER BY sort ",res.getId());
			map.put(res, reslist);
		}
		return map;
	}
	/**
	 * 根据pid 查询下面的所有资源
	 * @param pid
	 * @return
	 */
	public List<AdminRes>  getResByPid(Integer pid){
		return find("select * from t_admin_res where pid=? order by sort",pid); 
	}
	
	public Map<AdminRes,List<AdminRes>> getResByUserId(Integer user_id){
		Map<AdminRes,List<AdminRes>> map =new LinkedHashMap<AdminRes, List<AdminRes>>();
		//查询总共多少的权限
		List<AdminRoleRes> rolelist = AdminRoleRes.dao.find("select role.res_id from t_admin_role_res as role , t_admin_res res   where   role.role_id in(select role_id from t_admin_user_role where user_id=?) and res.id=role.res_id  and  res.pid  is not null  and res.type=1",user_id);
		Map<Integer, Integer> rolemap = new HashMap<Integer, Integer>();
		for(AdminRoleRes res : rolelist){
			rolemap.put(res.getResId(), res.getResId());
		}
		List<AdminRes> list = dao.find("select DISTINCT res.*from t_admin_role_res as role , t_admin_res res    where res.id=role.res_id  and role.role_id in(select role_id from t_admin_user_role where user_id=?)  and   res.pid  is null ",user_id);
		List<AdminRes>  inlist = new ArrayList<AdminRes>();
		for(AdminRes res :   list ){
			List<AdminRes> reslist =  dao.find("select * from t_admin_res where pid=? and type=1 ORDER BY sort ",res.getId());
			//循环判断删除
			for(int i = 0 ; i<reslist.size();i++){
				AdminRes r = reslist.get(i); 
				if(rolemap.get(r.getId())==null){
					
				}else{
					inlist.add(r);
				}
			}
			map.put(res, inlist);
			inlist = new ArrayList<AdminRes>();
		}
		return map;
	}
	/**
	 * 根据权限组ID 查询 这个权限组支持那些URL功能
	 * @param roleid
	 */
	public Map<String, String> findResUrlByUserId(Integer userid){
		String sql = "select res.url,res.id from t_admin_role_res as role , t_admin_res as res   where role.role_id in (select role_id from t_admin_user_role where user_id=?)  and role.res_id=res.id ";//and res.pid is not null
		List<AdminRes> lsit =  dao.find(sql,userid);
		Map<String, String>  map   =new HashMap<String, String>();
		for(AdminRes res :lsit){
			map.put(res.getUrl(), res.getId()+"");
		}
		return map;
	}
	

	
}
